package net.studio.simpletetris.control;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.view.SurfaceHolder;

public abstract class DrawThread extends Thread {
	private SurfaceHolder surfaceHolder;
	private boolean runFlag = false;

	public DrawThread(SurfaceHolder surfaceHolder, Resources resources) {
		this.surfaceHolder = surfaceHolder;
	}

	public void setRunning(boolean run) {
		runFlag = run;
	}

	@Override
	public void run() {
		Canvas canvas;

		while (runFlag) {

			canvas = null;

			try {
				canvas = surfaceHolder.lockCanvas(null);
				if (canvas != null) {
					synchronized (surfaceHolder) {
						this.renderLoop(canvas);
					}
				}
			} finally {
				if (canvas != null) {
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
			}
			try {
				sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return;
	}

	abstract public void renderLoop(Canvas canvas);
}
